// This class fires up a timeout
package com.bren.android.androidproject;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class TimeoutActivity extends Activity implements AdapterView.OnItemSelectedListener{
	private static final String PREFS_NAME="PrefsFile";
	private TextView selection;
	private String[] timers;
	private int[] values;
	private String timeout;
	private int time;// length of timeout
		
	public void onCreate(Bundle savedInstance) {
		super.onCreate(savedInstance);
		setContentView(R.layout.main2);
		timers=getResources().getStringArray(R.array.timers);
		values=getResources().getIntArray(R.array.timerMilliSeconds);
		
	    selection=(TextView)findViewById(R.id.selection);
		createSpinner();	
	}
	
	public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
		selection.setText("Current Selection: " + timers[position]);
		timeout=timers[position].toString();
		if(!timeout.equals("0 mins")){
			time = values[position];
			SharedPreferences prefs=getSharedPreferences(PREFS_NAME,0);
			SharedPreferences.Editor ed = prefs.edit();
			ed.putString("timeout", String.valueOf(time));
			ed.commit();
			
			AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
			audioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
		
			AlarmManager alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
			long triggerTime= SystemClock.elapsedRealtime() + time;
			int type = AlarmManager.ELAPSED_REALTIME;
			Intent intent= new Intent("com.barrybren.android.androidproject.TIME_EVENT");
			intent.putExtra("com.barrybren.android.androidproject.message","on");
			PendingIntent operation=PendingIntent.getBroadcast(this, 0, intent, 0);
			alarmManager.set(type, triggerTime, operation);
			Toast.makeText(this, "Timeout Set: " + timeout, Toast.LENGTH_SHORT).show();
		}
	}
		
	public void onNothingSelected(AdapterView<?> parent) {
		selection.setText("Current Selection: None");
	}
	
	private void createSpinner() {
		Spinner spin = (Spinner)findViewById(R.id.spinner);
		ArrayAdapter<String> a = new ArrayAdapter<String>(TimeoutActivity.this,
				android.R.layout.simple_spinner_item,
				timers);
		a.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);
		spin.setAdapter(a);
		spin.setOnItemSelectedListener(this);
	}
	
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if (time != 0) {
			outState.putString("timeout", Integer.toString(time));
		}		
	}
	protected void restoreMe(Bundle savedInstanceState) {
		time = 0;
		if (savedInstanceState!=null) {
			String timeout = savedInstanceState.getString("timeout");
			if (timeout!=null){
				time = Integer.parseInt(timeout);
			}
		}	
	}
	public void onConfigurationChanged(Configuration newConfing) {	
	}
	protected void onStop() {
		super.onStop();
	}
}
